package com.lh.bootswagger.controller;

import com.lh.bootswagger.entity.Student;
import com.lh.bootswagger.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lh
 * @Description: student相关控制层
 * @date 2021/3/24 11:54
 */
@RestController
@RequestMapping("/student")
@Api(value = "学生操作接口", tags = "学生操作接口")
public class StudentController {
	@Autowired
	private StudentService studentService;

	@ApiOperation(value = "保存新增学生信息", notes = "保存新增学生信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "sId", value = "主键", required = true, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "sName", value = "学生名称", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "sBirth", value = "学生生日", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "sSex", value = "学生性别", required = false, paramType = "query", dataType = "String")
	})
	@GetMapping("/saveOne")
	public String saveOne(String sId, String sName, String sBirth, String sSex) {
		Student student = new Student(sId, sName, sBirth, sSex);
		studentService.saveOne(student);
		return "success";
	}

	@ApiOperation(value = "删除学生信息", notes = "根据主键删除学生信息")
	@ApiImplicitParam(name = "sId", value = "主键", required = true, paramType = "query", dataType = "String")
	@GetMapping("/delStudentBySid")
	public String delStudentBySid(String sId) {
		studentService.delStudentBySid(sId);
		return "success";
	}

	@ApiOperation(value = "修改学生信息", notes = "根据主键修改学生信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "sId", value = "主键", required = true, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "sName", value = "学生名称", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "sBirth", value = "学生生日", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "sSex", value = "学生性别", required = false, paramType = "query", dataType = "String")
	})
	@GetMapping("/updateStudent")
	public String updateStudent(String sId, String sName, String sBirth, String sSex) {
		Student student = new Student(sId, sName, sBirth, sSex);
		studentService.updateStudent(student);
		return "success";
	}

	@ApiOperation(value = "查找所有学生信息", notes = "查找所有学生信息")
	@GetMapping("/findAllStudent")
	public String findAllStudent() {
		List<Student> studentList = studentService.findAllStudent();
		studentList.forEach(student -> {
			System.out.println(student);
		});
		return "success";
	}

	@ApiOperation(value = "查找学生信息", notes = "根据主键查找学生信息")
	@ApiImplicitParam(name = "sId", value = "主键", required = true, paramType = "query", dataType = "String")
	@GetMapping("/findStudentBySid")
	public String findStudentBySid(String sId) {
		Student student = studentService.findStudentBySid(sId);
		System.out.println(student);
		return "success";
	}
}
